package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;

/**
 * Created by huanghaosheng on 2021/4/13 19:51
 * 对讲耳机型号处理器
 */
public class IntercomHeadsetModelAttrHandler extends AbstractAttrHandler<KiteProdInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void after(KiteAttr attr, KiteProdInst inst, AttrData data) {
        String offerInstId = querySceneInstService.getSceneInst(data.getSceneInstId()).getRootInstId();
        List<KiteProdInst> kiteProdInstList = querySceneInstService
            .listProdInstsFromOfferInst(data.getCustId(), offerInstId);
        if (KiteListUtils.isNotEmpty(kiteProdInstList)) {
            dealDjModelAttr(attr, data, kiteProdInstList.get(0), inst);
        }
    }

    /**
     * @param attr
     * @param data
     * @param prodInst 主框架产品
     * @param inst
     */
    private void dealDjModelAttr(KiteAttr attr, AttrData data, KiteProdInst prodInst, KiteProdInst inst) {
        KiteAttr mainProdModelAttr = prodInst.getAttr("200002772"); //型号
        List<AttrValue> attrValues = AttrValueCache.get(attr.getAttrId()); //下拉列表规格数据
        if (!KiteObjectUtils.isEmpty(mainProdModelAttr) && KiteStringUtils
            .isEqual("100007529", mainProdModelAttr.getValue())) { //主产品型号选择POC060时,对讲耳机的型号更新下拉值
            attrValues = attrValues.stream()
                .filter(t -> KiteStringUtils.equalsAny(t.getAttrValue(), "110007336", "110007337", "110007338"))
                .collect(Collectors.toList()); //POC060（普通版）/POC060（空气导管版）/POC060（手持版）
            if (CollectionUtils.isNotEmpty(attrValues)) {
                attr.setAttrValues(KiteBeanUtils.copyPropsForList(attrValues, KiteAttrValue.class));
            }
            attr.setIsEdit("T");
            String extParam = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "entry");
            boolean flag = true;
            if ("sceneTempl".equals(data.getExtAttributes().get("entry")) || "sceneTempl".equals(extParam)) {
                flag = false;
            }
            if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(data.getServiceOfferId()) && !KeyConsts.IFTRUE_T.equals(
                querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE_SCENE))) {
                flag = false;
            }
            if (flag) {
                super.set(attr, inst, "110007336", data);
            }
        }
        else {
            attr.setIsEdit("F");
            if (!KiteStringUtils.isEqual("110006936", mainProdModelAttr.getValue())) { //型号
                attr.setIsEdit("F");
                super.set(attr, inst, mainProdModelAttr.getValue(), data);
            }
            else {
                attr.setIsEdit("T");
                super.set(attr, inst, "100007525", data);
            }
        }
    }
}
